package com.nbsaas.boot.otp.ext.resource;

import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
import org.springframework.stereotype.Service;

@Service
public class TotpService {

    private final GoogleAuthenticator gAuth = new GoogleAuthenticator();

    // 生成密钥
    public String generateSecretKey() {
        GoogleAuthenticatorKey key = gAuth.createCredentials();
        return key.getKey();
    }

    // 验证OTP
    public boolean validateCode(String secretKey, int code) {
        return gAuth.authorize(secretKey, code);
    }


    public static void main(String[] args) {
        TotpService totpService = new TotpService();
        String secretKey = totpService.generateSecretKey();
        System.out.println("secretKey:" + secretKey);

        boolean result = totpService.validateCode("SYYSHDCRU4ABOW3E", 911880);
        System.out.println("result:" + result);
    }
}
